package com.segi.view.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.segi.view.R;

/**
 * 这个类封装了下拉刷新的布局
 * 
 * @author liangzx
 * @version [segi, 2013-12-23]
 */
public class HeaderLoadingLayoutForUhome extends LoadingLayout {

    /**Header的容器*/
    private RelativeLayout mHeaderContainer;
    /**下拉第一战图*/
    private ImageView mFirstImg;
    /**进度条*/
    private ImageView mProgressBar;
    /**状态提示TextView*/
    private TextView mHintTextView;
    /**第一张图的高度**/
    private int mFirstImgHeight;
    /**第一张图的宽度**/
    private int mFirstImgWidth;
    /**
     * 
     */
    private AnimationDrawable mProgressAnim;
    
    /**
     * 构造方法
     * 
     * @param context context
     */
    public HeaderLoadingLayoutForUhome(Context context) {
        super(context);
        init(context);
    }

    /**
     * 构造方法
     * 
     * @param context context
     * @param attrs attrs
     */
    public HeaderLoadingLayoutForUhome(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     * 
     * @param context context
     */
    private void init(Context context) {
        mHeaderContainer = (RelativeLayout) findViewById(R.id.pull_to_refresh_header_content);
        mHintTextView = (TextView) findViewById(R.id.pull_to_refresh_header_hint_textview);
        mFirstImg = (ImageView) findViewById(R.id.pull_to_refresh_header_first);
        mProgressBar = (ImageView) findViewById(R.id.pull_to_refresh_header_progressbar);

        mProgressAnim = (AnimationDrawable) mProgressBar.getDrawable();
        
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);  
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);  
        mFirstImg.measure(w, h);
        mFirstImgHeight = mFirstImg.getMeasuredHeight();
        mFirstImgWidth = mFirstImg.getMeasuredWidth();
    }

    @Override
    public void setLastUpdatedLabel(CharSequence label) {
        // 如果最后更新的时间的文本是空的话，隐藏前面的标题
    }

    @Override
    public int getContentSize() {
        if (0 < mFirstImgHeight){
            return mFirstImgHeight;
        }
//        if (null != mHeaderContainer) {
//            return mHeaderContainer.getHeight();
//        }
        
        return (int) (getResources().getDisplayMetrics().density * 60);
    }
    
    @Override
    protected View createLoadingView(Context context, AttributeSet attrs) {
        View container = LayoutInflater.from(context).inflate(R.layout.pull_updown_header_for_uhome, null);
        return container;
    }
    
    @Override
    protected void onStateChanged(State curState, State oldState) {
        super.onStateChanged(curState, oldState);
    }

    @Override
    protected void onReset() {
    	RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) mFirstImg.getLayoutParams();
		params.height = mFirstImgHeight;
		params.width = mFirstImgWidth;
		mFirstImg.setLayoutParams(params);
    	stopAnim();
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_normal);
    }
    
    @Override
    public void onPull(float scale) {
    	if (getState().equals(State.REFRESHING) || getPreState().equals(State.REFRESHING)) {
			return;
		}
    	if (scale > 1) {
			RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) mFirstImg.getLayoutParams();
			params.height = (int) (scale * mFirstImgHeight);
			params.width = mFirstImgWidth;
			mFirstImg.setLayoutParams(params);
		}
    }

    @Override
    protected void onPullToRefresh() {
    	stopAnim();
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_normal);
    }

    @Override
    protected void onReleaseToRefresh() {
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_ready);
    }

    @Override
    protected void onRefreshing() {
        startAnim();
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_loading);
    }
    
    /**
     * 开始动画
     */
    private void startAnim()
    {
    	RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) mFirstImg.getLayoutParams();
		params.height = mFirstImgHeight;
		params.width = mFirstImgWidth;
		mFirstImg.setLayoutParams(params);
    	mFirstImg.setVisibility(View.INVISIBLE);
    	mProgressBar.setVisibility(View.VISIBLE);
    	if (!mProgressAnim.isRunning()) {
    		mProgressAnim.start();
		}
    }
    
    /**
     * 暂停动画
     */
    private void stopAnim()
    {
		mFirstImg.setVisibility(View.VISIBLE);
    	mProgressBar.setVisibility(View.INVISIBLE);
    	if (mProgressAnim.isRunning()) {
    		mProgressAnim.stop();
		}
    }
}
